<template>
  <div class="login-container">
    <!-- 左侧信息面板 -->
    <div class="info-panel">
      <h2 class="welcome">欢迎使用</h2>
      <p class="subtitle">管理系统</p>
      <div class="image-carousel">
        <img :src="images[currentIndex]" alt="Carousel" />
      </div>
      <div class="decor-icons">
        <span class="icon i1"></span>
        <span class="icon i2"></span>
        <span class="icon i3"></span>
      </div>
    </div>

    <!-- 微粒背景 -->
    <canvas id="particle-canvas" class="particle-canvas"></canvas>

    <!-- 登录表单靠右（中间偏右） -->
    <div class="login-form">
      <img src="/logo.png" alt="Logo" class="logo" />
      <div class="title">管理系统登录</div>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="form-inner">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="User" placeholder="用户名" class="input-field" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="Lock" type="password" placeholder="密码" class="input-field" />
        </el-form-item>
        <el-form-item class="extras">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <span class="forgot" @click="goForgot">忘记密码?</span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="social-login">
        <span>其他方式登录</span>
        <el-button circle icon="LogoGithub" @click="socialLogin('github')" />
        <el-button circle icon="Wechat" @click="socialLogin('wechat')" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api'

const router = useRouter()

// 轮播图片与计数
const images = ['/images/pic1.jpg', '/images/pic2.jpg', '/images/pic3.jpg']
const currentIndex = ref(0)
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({ username: '', password: '', remember: false })
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = () => {
  loginFormRef.value.validate(async valid => {
    if (!valid) return
    loading.value = true
    try {
      const { code, data, msg } = (await login({ username: loginForm.username, password: loginForm.password })).data
      if (code !== 0) {
        ElMessage.error(msg || '登录失败')
        return
      }
      localStorage.setItem('adminToken', data.token)
      ElMessage.success('登录成功')
      router.push('/admin/dashboard')
    } catch (error) {
      console.error('登录异常:', error)
      ElMessage.error(error.response?.data?.msg || '登录请求失败')
    } finally {
      loading.value = false
    }
  })
}
const goForgot = () => router.push('/forgot-password')
const socialLogin = provider => ElMessage.info(`社交登录：${provider}`)

// Canvas 粒子动画 & 图片轮播
onMounted(() => {
  // 轮播
  setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % images.length
  }, 3000)

  // 粒子效果
  const canvas = document.getElementById('particle-canvas')
  const ctx = canvas.getContext('2d')
  let w = canvas.width = window.innerWidth
  let h = canvas.height = window.innerHeight
  const particles = []
  function init() {
    particles.length = 0
    for (let i = 0; i < 100; i++) {
      particles.push({ x: Math.random() * w, y: Math.random() * h, r: Math.random() * 2 + 1, d: Math.random() * 50 })
    }
  }
  function draw() {
    ctx.clearRect(0, 0, w, h)
    ctx.fillStyle = 'rgba(255,255,255,0.2)'
    particles.forEach(p => {
      ctx.beginPath()
      ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2)
      ctx.fill()
    })
    update()
    requestAnimationFrame(draw)
  }
  let angle = 0
  function update() {
    angle += 0.01
    particles.forEach(p => {
      p.y += Math.cos(angle + p.d) + 0.5 + p.r / 2
      p.x += Math.sin(angle) * 2
      if (p.x > w + 5 || p.x < -5 || p.y > h) {
        p.x = Math.random() * w
        p.y = -10
      }
    })
  }
  window.addEventListener('resize', () => {
    w = canvas.width = window.innerWidth
    h = canvas.height = window.innerHeight
    init()
  })
  init()
  draw()
})

</script>

<style scoped>
.login-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea, #764ba2);
  display: flex;
  justify-content: right;
  align-items: center;
}

/* 左侧信息面板 */
.info-panel {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(255, 255, 255, 0.85);
  text-align: left;
}
.welcome {
  font-size: 32px;
  margin: 0;
  font-weight: 500;
}
.subtitle {
  font-size: 24px;
  margin: 8px 0 24px;
}
.image-carousel img {
  width: 200px;
  height: auto;
  border-radius: 8px;
  margin-bottom: 16px;
}
.decor-icons {
  display: flex;
  gap: 12px;
}
.decor-icons .icon {
  width: 20px;
  height: 20px;
  border-radius: 4px;
}
.decor-icons .i1 {
  background-color: rgba(255, 255, 255, 0.5);
}
.decor-icons .i2 {
  background-color: rgba(255, 255, 255, 0.4);
}
.decor-icons .i3 {
  background-color: rgba(255, 255, 255, 0.3);
}

.particle-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.login-form {
  position: relative;
  z-index: 1;
  margin-right: 500px;
  width: 380px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  text-align: center;
}
.logo {
  width: 60px;
  margin-bottom: 16px;
}
.title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 24px;
  color: #333;
}
.input-field ::v-deep .el-input__inner {
  border-radius: 6px;
  height: 46px;
  font-size: 14px;
  padding-left: 14px;
}
.extras {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}
.forgot {
  font-size: 12px;
  color: #409EFF;
  cursor: pointer;
  margin-left: auto;
}
.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 6px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  border: none;
}
.login-button:hover {
  background: linear-gradient(90deg, #5563c1, #643b8c);
}
.social-login {
  margin-top: 24px;
  font-size: 12px;
  color: #888;
}
.social-login span {
  display: block;
  margin-bottom: 8px;
}
.social-login .el-button {
  margin: 0 8px;
}
</style>